package com.itlong.cloud.rabbitmq.config;

import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.utils.rabbitMQ.BaseConfig;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * <desc>
 *      支付服务消息队列配置.
 * </desc>
 *
 * @createDate 2018/09/23
 */
@Configuration
public class RabbitConfig extends BaseConfig {

    // 声明交换器
    @Bean
    TopicExchange topicExchange(){
        return new TopicExchange(RabbitMQConstant.RABBITMQ_PAY_EXCHANGE);
    }


    // 修改项目可提现余额,实时余额业务队列
    @Bean
    public Queue projectAmountQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_PROJECT_AMOUNT_MANAGER_QUEUE,true, false, false, map);
        return queue;
    }

    // 新增资金明细变动表(支付时)
    @Bean
    public Queue AmountChangeInfoQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_AMOUNT_CHANGE_INFO_MANAGER_QUEUE,true, false, false, map);
        return queue;
    }

    // 更新用户和房间欠费状态
    @Bean
    public Queue userRoomForbiddenQueue() {
        Map<String, Object> map = new HashMap<>();
        map.put("x-message-ttl", RabbitMQConstant.QUEUE_TTL);
        map.put("x-dead-letter-exchange", RabbitMQConstant.DEAD_LETTER_EXCHANGE);
        map.put("x-dead-letter-routing-key", RabbitMQConstant.MAIL_QUEUE_FAIL);
        Queue queue = new Queue(RabbitMQConstant.RABBITMQ_USER_ROOM_FORBIDDEN_QUEUE,true, false, false, map);
        return queue;
    }


    // 修改项目可提现余额,实时余额队列路由key
    @Bean
    Binding projectAmountQueueBinding() {
        return BindingBuilder.bind(projectAmountQueue()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_PROJECT_AMOUNT_ROUTING_KEY);
    }

    // 新增资金明细变动表队列路由key
    @Bean
    Binding AmountChangeInfoQueueBinding() {
        return BindingBuilder.bind(AmountChangeInfoQueue()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_AMOUNT_CHANGE_INFO_ROUTING_KEY);
    }

    // 更新用户和房间欠费状队列路由key
    @Bean
    Binding userRoomForbiddenQueueBinding() {
        return BindingBuilder.bind(userRoomForbiddenQueue()).to(topicExchange()).with(RabbitMQConstant.RABBITMQ_USER_ROOM_FORBIDDEN_ROUTING_KEY);
    }


}
